package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SignupConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.RegExUtil;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.Activity;
import com.ruoyi.system.domain.vo.IndexSchoolRole;
import com.ruoyi.system.service.IActivityService;
import org.springframework.amqp.rabbit.connection.RabbitUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Signup;
import com.ruoyi.system.service.ISignupService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 报名录取Controller
 * 
 * @author hyb
 * @date 2023-02-10
 */
@RestController
@RequestMapping("/signup")
public class SignupController extends BaseController
{
    @Autowired
    private ISignupService signupService;

    @Autowired
    private IActivityService activityService;

    /*
    * 查询某个活动的报名人
    * */

    @InnerAuth
    @GetMapping("/getSignUp")
    public AjaxResult getSignUpByActivityId(@RequestParam("activityId")String activityId){
        List<IndexSchoolRole> list=signupService.getSignUpByActivityId(activityId);
        return AjaxResult.success().put("list",list);

    }

    @InnerAuth
    @GetMapping("/personalSignUp")
    public AjaxResult getPersonalSignUp(@RequestParam("id")String id){
        Signup signup = new Signup();
        signup.setActivityId(Long.parseLong(id));
        signup.setUserId(SecurityUtils.getUserId());
        List<Signup> signups = signupService.selectSignupList(signup);
        if (signups.size()==0){
            return AjaxResult.error();
        }else{
            return AjaxResult.success().put("signUp",signups.get(0));
        }
    }


    /**
     * 查询报名录取列表
     */
    @RequiresPermissions("signup:signup:list")
    @GetMapping("/list")
    public TableDataInfo list(Signup signup)
    {
        //只能查询本账号作为负责人的活动
        signup.setActivityUserId(SecurityUtils.getUserId());
        startPage();
        List<Signup> list = signupService.selectSignupListAndCheckActivity(signup);
        return getDataTable(list);
    }

    /**
     * 导出报名录取列表
     */
    @RequiresPermissions("signup:signup:export")
    @Log(title = "报名录取", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Signup signup)
    {
        List<Signup> list = signupService.selectSignupList(signup);
        ExcelUtil<Signup> util = new ExcelUtil<Signup>(Signup.class);
        util.exportExcel(response, list, "报名录取数据");
    }

    /**
     * 获取报名录取详细信息
     */
    @RequiresPermissions("signup:signup:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(signupService.selectSignupById(id));
    }

    /**
     * 新增报名录取
     */
    @InnerAuth
    @Log(title = "报名录取", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Signup signup)
    {

        return toAjax(signupService.insertSignup(signup));
    }

    /**
     * 修改报名录取
     */
    @RequiresPermissions("signup:signup:edit")
    @Log(title = "报名录取", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Signup signup)
    {
        return toAjax(signupService.updateSignup(signup));
    }
    /**
     * 录取
     */
    @RequiresPermissions("signup:signup:addSign")
    @Log(title = "录取", businessType = BusinessType.UPDATE)
    @PutMapping("/addSign")
    public AjaxResult addSign(@RequestParam("id")String id)
    {
        if (!RegExUtil.isNumber(id)){
            return AjaxResult.error("id必须是数字且不能为空!");
        }
        Signup sg= signupService.selectSignupById(Long.parseLong(id));
        if (!SignupConstants.CREATE_STATUS.equals(sg.getStatus()) && !SignupConstants.REMOVE_STATUS.equals(sg.getStatus())){
            return AjaxResult.error("该记录未在创建或落选状态,无法进行录取或补录工作");
        }
        Activity activity = activityService.selectActivityById(sg.getActivityId());
        if (activity==null){
            return AjaxResult.error("活动不存在");
        }

        Long userId = activity.getUserId();
        if (!SecurityUtils.getUserId().equals(userId)){
            return AjaxResult.error("报名与录取功能只能由负责人去处理");
        }



        Signup signup = new Signup();
        signup.setId(Long.parseLong(id));
        signup.setStatus(SignupConstants.ADD_STATUS);
        //处理者
        signup.setVerifyUserId(SecurityUtils.getUserId());
        signup.setPf3(SecurityUtils.getUsername());
        signup.setVerifyTime(DateUtils.getNowDate());

        return toAjax(signupService.updateSignup(signup));
    }

    @RequiresPermissions("signup:signup:removeSign")
    @Log(title = "落选", businessType = BusinessType.UPDATE)
    @PutMapping("/removeSign")
    public AjaxResult removeSign(@RequestParam("id")String id)
    {
        if (!RegExUtil.isNumber(id)){
            return AjaxResult.error("id必须是数字且不能为空!");
        }

        Signup sg = signupService.selectSignupById(Long.parseLong(id));
        if (sg==null){
            return AjaxResult.error("该学生未报名");
        }
        if (!SignupConstants.CREATE_STATUS.equals(sg.getStatus())){
            return AjaxResult.error("该记录未在创建状态,无法落选!");
        }

        Activity activity = activityService.selectActivityById(sg.getActivityId());
        if (activity==null){
            return AjaxResult.error("活动不存在");
        }

        Long userId = activity.getUserId();
        if (!SecurityUtils.getUserId().equals(userId)){
            return AjaxResult.error("报名与录取功能只能由负责人去处理");
        }

        Signup signup = new Signup();
        signup.setId(Long.parseLong(id));
        signup.setStatus(SignupConstants.REMOVE_STATUS);

        //处理者
        signup.setVerifyUserId(SecurityUtils.getUserId());
        signup.setPf3(SecurityUtils.getUsername());
        signup.setVerifyTime(DateUtils.getNowDate());
        return toAjax(signupService.updateSignup(signup));
    }






    /**
     * 删除报名录取
     */
    @RequiresPermissions("signup:signup:remove")
    @Log(title = "报名录取", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(signupService.deleteSignupByIds(ids));
    }
}
